export default {
  props: {
    //值
    value: {
      type: [Array, Object, String, Number, Boolean]
    },
    // 问题结构 非一定注入
    item: {
      type: Object,
      default: () => {
      }
    },
    // 考试
    examConfig: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      config: null,
      changeValue: this.value,
      newOptions:this.getOptions() || []
    }
  },
  computed: {},
  created() {
    if (this.item) {
      this.config = this.item.config
    }
  },
  watch: {
    changeValue(val) {
      this.$emit('input', val)
    },
    value(val) {
      this.changeValue = val
    },
    'item.config.options'(val){
      this.getOptions()
    },
    'item.config.randomOption'(val){
      this.getOptions()
    },
    'item.config.selectRandomOption'(val){
      
      this.getOptions()
      this.preSelectRandomOption=JSON.parse(JSON.stringify(this.item.config.selectRandomOption))
      // this.$store.dispatch('form/uploadPreSelectRandomOption', this.item.config.selectRandomOption)
    }
  },
  mounted() {
  },
  methods: {
    getOptions() {
      if(this.item && this.item.config){
        if(this.item.config.randomOption){
          if(this.item.config.selectRandomOption.length){
            if(this.preSelectRandomOption != this.item.config.selectRandomOption){
              this.item.config.options.forEach(item => {
                if(this.item.config.selectRandomOption.includes(item.value)){
                  item.isRandom=true
                }else{
                  item.isRandom=false
                }
              });
        
              var randomOptionList = this.item.config.options.filter(o => o.isRandom);
              var newOptionList = randomOptionList.sort(function() {
                return .5 - Math.random();
              });
              this.item.config.options.forEach((item, index) => {
                if (!item.isRandom) {
                  newOptionList.splice(index, 0, item);
                }
              });
              
            }
            this.newOptions=newOptionList
            return newOptionList
          }else{
            this.newOptions=this.item.config.options
            return this.item.config.options
          }
          
        }else{
          this.newOptions=this.item.config.options
          return this.item.config.options
        }
      }
      
    }
  }
}
